import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';

export interface Result<T> {
  code: number;
  data: T;
  message?: string;
}

@Injectable({
  providedIn: 'root'
})
export class HttpService {

  constructor(private http: HttpClient) { }

  login(user) {
    return this.http.post<Result<any>>(environment.server + '/login', user).toPromise()
  }

  logout(refreshToken) {
    const headers = new HttpHeaders().append('Refresh-Token', refreshToken)
    return this.http.get<Result<any>>(environment.server + '/logout', { headers: headers }).toPromise()
  }

  renewalToken(refreshToken) {
    const headers = new HttpHeaders().append('Refresh-Token', refreshToken)
    return this.http.get<Result<any>>(environment.server + '/token', { headers: headers }).toPromise()
  }

  getLoginUserInfo() {
    return this.http.get<Result<any>>(environment.server + '/userinfo').toPromise()
  }

  getFriends() {
    return this.http.get<Result<any>>(environment.server + '/user/friends').toPromise()
  }

  getFriend(friendId) {
    return this.http.get<Result<any>>(environment.server + '/user/friend' + friendId).toPromise()
  }

  getContactsBytelephone(telephones) {
    return this.http.post<Result<any[]>>(environment.server + '/user/telephone-users', telephones).toPromise()
  }

  addContact(contact) {
    return this.http.post<Result<any>>(environment.server + '/user/friend/add', contact).toPromise()
  }

  getMessages() {
    // const friends = [
    //   {id: 1, name: '一家亲', image: './assets/images/avatar-ben.png', message: '366公交车有点毛病， 我8点25就到了车站等着哒， 8点50车才到。 然后就是果断迟到20分钟。', unread: 100, time: 1619484054000, nohup: false},
    //   {id: 2, name: '聪哥', image: './assets/images/avatar-finn.png', message: 'http://192.168.1.100:8100/tabs/chats', unread: 6, time: 1619419134000, nohup: false},
    //   {id: 3, name: '刘艳', image: './assets/images/avatar-han.png', message: '小瑞睡啦', unread: 78, time: 1617326106995, nohup: false},
    //   {id: 4, name: '装逼聊天室', image: './assets/images/avatar-leia.png', message: '保举一人，邓溪', unread: 1, time: 1618423693450, nohup: false},
    //   {id: 5, name: '培英幼儿园大班', image: './assets/images/avatar-luke.png', message: '家长们，下午好！作业已发到掌通家园，请您抽时间去查看。', unread: 10, time: 1617430591025, nohup: true},
    //   {id: 6, name: 'A区兴盛优选银馨家园芙蓉兴盛', image: './assets/images/avatar-poe.png', message: '红姨，有三室两厅的房出租吗？', unread: 76, time: 1617666329377, nohup: false},
    //   {id: 7, name: '帮你省银馨家园徐娟', image: './assets/images/avatar-rey.png', message: '朋友们：“十荟团”买50减12，买100减20.......活动力很大，请观注', unread: 0, time: 1617953966713, nohup: true},
    //   {id: 8, name: '2577生鲜🥬银馨家园34店顾客群', image: './assets/images/avatar-yoda.png', message: '大惊喜！大惊喜！进店消费🈵58元送火锅底料一包！欢迎家人们进店选购！所有活动晚上6点截止。', unread: 100, time: 1609051745000, nohup: true},
    //   {id: 9, name: '银馨家园B区服务群', image: './assets/images/avatar-finn.png', message: 'b10栋2单元电梯已恢复', unread: 14, time: 1619144288691, nohup: false},
    //   {id: 10, name: '银馨家园A区服务群', image: './assets/images/avatar-leia.png', message: '@所有人  请各位业主谨防电信诈骗', unread: 35, time: 1617758690840, nohup: false},
    //   {id: 11, name: '扯淡', image: './assets/images/avatar-han.png', message: '你抬得起，我就抬得起撒，要恰国碗饭', unread: 9, time: 1618063407469, nohup: false},
    //   {id: 12, name: '长沙互联网主力军', image: './assets/images/avatar-poe.png', message: '哈哈，招前端。', unread: 156, time: 1617891870301, nohup: false},
    //   {id: 13, name: '聪明妹', image: './assets/images/avatar-luke.png', message: '不要WIFI的款', unread: 96, time: 1619249153281, nohup: false},
    //   {id: 14, name: '夜战', image: './assets/images/avatar-ben.png', message: '刚换好衣服搞不了', unread: 7, time: 1619379777019, nohup: false}
    // ]
    // return Promise.resolve(friends)
    return this.http.get<Result<any>>(environment.server + '/message/brief').toPromise()
  }

  uploadPhoto(photo: Blob) {
    var headers = new HttpHeaders().append('access_token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9')
    const formData = new FormData();
    formData.append('file', photo, Date.now() + '.jpg');
    return this.http.post<Result<any>>(environment.server + '/user/photo/upload', formData, { headers: headers }).toPromise()
  }
}
